package in.ac.dei.edrp.cms.controller.studentregistration;


import in.ac.dei.edrp.cms.dao.studentregistration.CheckDuplicateStudentDAO;
import in.ac.dei.edrp.cms.domain.studentregistration.StudentInfoGetter;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class DuplicateStudentListPDFView extends AbstractPdfView {
	
	private CheckDuplicateStudentDAO checkDuplicateStudentDAO;	

//	public void setFinalSemesterResultStatisticsDao(
//			CheckDuplicateStudentDAO checkDuplicateStudentDAO) {
//		this.checkDuplicateStudentDAO = checkDuplicateStudentDAO;
//	}



	@SuppressWarnings("unchecked")
	protected void buildPdfDocument(Map model,Document document,PdfWriter writer, HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		List <StudentInfoGetter> duplicateStudentList;
		String directoryName = null;
		File file;
		directoryName = getServletContext().getRealPath("/")+"PDF";
		
		HttpSession session=request.getSession(true);
		
		file = new File(directoryName);
	
		directoryName = directoryName +"/"+session.getAttribute("universityId");
		file = new File(directoryName);
		file.mkdir();
		
		directoryName = directoryName+"/"+session.getAttribute("startDate")+"  /  "+session.getAttribute("endDate");
		file = new File(directoryName);		
		file.mkdir();		
		System.out.println(" sfdsds " +directoryName);
		writer = PdfWriter.getInstance(document,new FileOutputStream(directoryName+"/Duplicate Student List"+".pdf"));
		document.setPageSize(PageSize.A4.rotate());
		document.open();
		
		String headerText1 = "Duplicate Students List :"+ session.getAttribute("startDate")+"-"+session.getAttribute("endDate");
		Paragraph header1 = new Paragraph(headerText1,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));		
		
		String printingDate = new Date().toString();
		Paragraph header2 = new Paragraph(printingDate,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));
		
		header1.setAlignment(Element.ALIGN_CENTER);
		document.add(header1);
		document.add(header2);
		
		duplicateStudentList = (List <StudentInfoGetter>) model.get("duplicateStudentList");
		
		StudentInfoGetter finalSemesterResultStatistics=new StudentInfoGetter();
		finalSemesterResultStatistics.setProgramCourseKey(duplicateStudentList.get(0).getProgramCourseKey());
		finalSemesterResultStatistics.setProgramId(duplicateStudentList.get(0).getProgramId());
		finalSemesterResultStatistics.setBranchId(duplicateStudentList.get(0).getBranchId());
		finalSemesterResultStatistics.setSpecializationId(duplicateStudentList.get(0).getSpecializationId());
		finalSemesterResultStatistics.setSemesterCode(duplicateStudentList.get(0).getSemesterCode());
		
		Font cellFont = new Font(Font.HELVETICA, 8);
		Table studentTable = new Table(15,3);
				
				Cell c1 = new Cell(new Phrase(" Registration Number ",cellFont));
				c1.setColspan(2);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Student Name \n ",cellFont));
				c1.setColspan(2);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Father's Name ",cellFont));
				c1.setColspan(2);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);		
				
				c1 = new Cell(new Phrase(" Mother's Name ",cellFont));
				c1.setColspan(2);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Date of Birth ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Category ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Gender ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Program ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Branch ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Specialization ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Semester ",cellFont));
				c1.setColspan(1);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				for(int i=0;i<duplicateStudentList.size();i++){
									
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getRegistrationNumber(),cellFont));
					c1.setColspan(2);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getStudentName(),cellFont));
					c1.setColspan(2);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getFatherName(),cellFont));
					c1.setColspan(2);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getMotherName(),cellFont));
					c1.setColspan(2);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getDateOfBirth(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getCategory(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getGender(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getProgramName(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getBranchName(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getNewSpecialization(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(duplicateStudentList.get(i).getSemester(),cellFont));
					c1.setColspan(1);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
				}
			
			studentTable.setBorderWidth(1);		
			studentTable.endHeaders();		
			document.add(studentTable);
			document.close();		
	}
	
}

